package nl.tomcee.skinprops;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.DateFormatter;
import javax.swing.text.DefaultFormatterFactory;
import net.miginfocom.swing.MigLayout;
import se.datadosen.jalbum.Frog;
import se.datadosen.jalbum.JAlbumContext;
import se.datadosen.jalbum.SkinProperties;
import se.datadosen.util.JComponentHolder;
import se.datadosen.util.ManagesImport;
import se.datadosen.util.PropertyBinder;

/**
 * Reads and writes the skin.properties file
 * @since jalbum 8.7
 * @author Tom
 * @version 1.7
 */
public class SkinPropsEditor extends JFrame {

    final static String APP_NAME = "Skin Properties Editor";
    final static String VERSION = "2.0";
    final static String AUTHOR = "Tom and David";
    final String SIMPLE_NAME = getClass().getSimpleName();
    /**
     ***************
     * init fields *
     ***************
     */
    private JAlbumContext context = JAlbumContext.getInstance();
    private String skin = context.getSkinDir().getName();
    private String accountProfileName = context.getEngine().getAccountProfileName();
    private SkinProperties skinProperties = context.getEngine().getSkinProperties();
    private Date currentDate = new Date();
    private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    private DefaultFormatterFactory dff = new DefaultFormatterFactory(new DateFormatter(formatter));
    private String now = formatter.format(currentDate);

    public class JContentPanel extends JPanel implements JComponentHolder, ManagesImport {

        public JTextField title = new JTextField("", 40);
        public JTextField supportForum = new JTextField("", 40);
        public JTextField author = new JTextField("", 40);
        public JCheckBox autoWidgetInjection = new JCheckBox();
        public JCheckBox compatibilityMode = new JCheckBox();
        public JCheckBox fullSiteSupport = new JCheckBox();
        public JCheckBox fullVideoSupport = new JCheckBox();
        public JTextField company = new JTextField("", 40);
        public JTextArea description = new JTextArea() {

            {
                setRows(5);
                setColumns(40);
                setLineWrap(true);
                setWrapStyleWord(true);
                setFont(new java.awt.Font("Tahoma", 0, 11));
            }
        };
        public JScrollPane spDescription = new JScrollPane() {

            {
                setViewportView(description);
            }
        };
        public JTextField homePage = new JTextField("", 40);
        public JTextField requiredJalbumVersion = new JTextField("", 40);
        public JTextField requiredJavaVersion = new JTextField("", 40);
        public JTextField username = new JTextField("", 40);
        public JTextField version = new JTextField("", 40);
        public JFormattedTextField creationDate = new JFormattedTextField(dff);
        public JFormattedTextField lastModifiedDate = new JFormattedTextField(dff);
        private JButton saveButton = new JButton("Save") {

            {
                addActionListener(new java.awt.event.ActionListener() {

                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        saveSkinProps();
                        dispose();
                    }
                });
            }
        };
        private JButton cancelButton = new JButton("Cancel") {

            {
                addActionListener(new java.awt.event.ActionListener() {

                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        dispose();
                    }
                });
            }
        };
        private JButton nowButton = new JButton("now!") {

            {
                addActionListener(new java.awt.event.ActionListener() {

                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        lastModifiedDate.setText(now);
                    }
                });
                addMouseListener(new java.awt.event.MouseAdapter() {

                    @Override
                    public void mouseEntered(java.awt.event.MouseEvent evt) {
                        setText(now);
                    }

                    @Override
                    public void mouseExited(java.awt.event.MouseEvent evt) {
                        setText("now!");
                    }
                });
            }
        };

        {
            setLayout(new MigLayout("wrap 2"));
            add(new JLabel(SkinProperties.TITLE));
            add(title);
            add(new JLabel(SkinProperties.VERSION));
            add(version);
            add(new JLabel(SkinProperties.AUTHOR));
            add(author);
            add(new JLabel(SkinProperties.CREATION_DATE));
            add(creationDate, "split 2");
            add(new JLabel("(YYYY-MM-DD)"));
            add(new JLabel(SkinProperties.LAST_MODIFIED_DATE));
            add(lastModifiedDate, "split 3");
            add(new JLabel("(YYYY-MM-DD)"));
            add(nowButton);
            add(new JLabel(SkinProperties.AUTO_WIDGET_INJECTION));
            add(autoWidgetInjection);
            add(new JLabel(SkinProperties.COMPATIBILITY_MODE));
            add(compatibilityMode);
            add(new JLabel(SkinProperties.FULL_SITE_SUPPORT));
            add(fullSiteSupport);
            add(new JLabel(SkinProperties.FULL_VIDEO_SUPPORT));
            add(fullVideoSupport);
            add(new JLabel(SkinProperties.REQUIRED_JALBUM_VERSION));
            add(requiredJalbumVersion);
            add(new JLabel(SkinProperties.REQUIRED_JAVA_VERSION));
            add(requiredJavaVersion);
            add(new JLabel(SkinProperties.COMPANY));
            add(company);
            add(new JLabel(SkinProperties.USERNAME));
            add(username);
            add(new JLabel(SkinProperties.HOME_PAGE));
            add(homePage);
            add(new JLabel(SkinProperties.SUPPORT_FORUM));
            add(supportForum);
            add(new JLabel(SkinProperties.DESCRIPTION));
            add(spDescription);
            add(saveButton, "cell 1 13, split 2, align right");
            add(cancelButton);
        }

        public void importVariables(Map<String, Object> skinVariables) {
            if (!skinVariables.containsKey(SkinProperties.TITLE)) {
                skinVariables.put(SkinProperties.TITLE, skin);
            }
            if (!skinVariables.containsKey(SkinProperties.CREATION_DATE)) {
                skinVariables.put(SkinProperties.CREATION_DATE, now);
            }
            if (!skinVariables.containsKey(SkinProperties.LAST_MODIFIED_DATE)) {
                skinVariables.put(SkinProperties.LAST_MODIFIED_DATE, now);
            }
            if (!skinVariables.containsKey(SkinProperties.USERNAME) && !accountProfileName.equals("")) {
                skinVariables.put(SkinProperties.USERNAME, accountProfileName.substring(accountProfileName.lastIndexOf("- ") + 2));
            }
            if (!skinVariables.containsKey(SkinProperties.REQUIRED_JAVA_VERSION)) {
                skinVariables.put(SkinProperties.REQUIRED_JAVA_VERSION, "1.5");
            }            
        }
    }
    JContentPanel contentPanel = new JContentPanel();

    /**
     ****************
     * init methods *
     ****************
     */
    //
    /**
     *
     * @return menubar
     */
    private JMenuBar buildMenu() {
        JMenuBar menubar = new JMenuBar();
        JMenuItem aboutMenuItem = new JMenuItem("About");
        aboutMenuItem.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                new Frog().ribbit();
                String aboutText = "<html><b>" + APP_NAME
                        + "<br>version: " + VERSION
                        + "<br>author: " + AUTHOR
                        + "</b></html>";
                JOptionPane.showMessageDialog(rootPane, aboutText,
                        "About: " + SIMPLE_NAME,
                        JOptionPane.INFORMATION_MESSAGE, new ImageIcon(getClass().
                        getResource("tclogo.gif")));
            }
        });
        menubar.add(aboutMenuItem);
        return menubar;
    }

    private void getSkinProps() {
        PropertyBinder.syncUI(skinProperties.getProperties(), contentPanel);
    }

    private void saveSkinProps() {
        PropertyBinder.getProperties(contentPanel, skinProperties.getProperties());
        try {
            skinProperties.save();
            System.out.println(new Date() + " skin.properties saved succesfully.");
        } catch (IOException ex) {
            System.out.println(ex + " - failed to write skin.properties");
        }
    }

    /**
     *********************
     * application logic *
     *********************
     */
    public SkinPropsEditor() {
        setTitle(skin + " - " + APP_NAME);
        setIconImage(new ImageIcon(getClass().
                getResource("tclogo.gif")).getImage());
        setLocationByPlatform(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        setJMenuBar(buildMenu());
        getSkinProps();
        getContentPane().add(contentPanel);

        pack();
        setVisible(true);

        contentPanel.description.setCaretPosition(0);
        contentPanel.cancelButton.requestFocusInWindow();

        //System.out.println(SIMPLE_NAME + " loaded at " + new Date());
    }
}//

